package kr.co.goodwilldd.framework.security;

import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import javax.annotation.PostConstruct;

import kr.co.goodwilldd.framework.repository.DefaultRepository;
import kr.co.goodwilldd.framework.security.model.Authentication;
import kr.co.goodwilldd.framework.security.model.CustomUser;
import kr.co.goodwilldd.framework.security.model.CustomUserDetails;
import kr.co.goodwilldd.framework.security.model.UserRegistration;
import kr.co.goodwilldd.framework.security.model.UserRole;
import kr.co.goodwilldd.framework.security.repository.ISecurityRepository;
import kr.co.goodwilldd.framework.service.IServiceDefault;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

@Service
public class CustomUserDetailService implements UserDetailsService, IServiceDefault {

	@Autowired
	ISecurityRepository iSecurityRepository;
	
	@Autowired
	DefaultRepository defaultRepository;
	
	@Override
	@PostConstruct
	public void init() {
		defaultRepository.doCreateTableAuth();
		defaultRepository.doCreateTableAuthRole();
		defaultRepository.doCreateUniqueIndexAuthRole();
		defaultRepository.doInsertSuperUser();
		defaultRepository.doInsertSuperUserRole();
	}

	@Override
	@DependsOn(value = "iSecurityRepository")
	public CustomUserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		Vector<GrantedAuthority> userAuthorities = new Vector<GrantedAuthority>();
		ListIterator<UserRole> userRoleLiterator = getUserRole(username)
				.listIterator();
		while (userRoleLiterator.hasNext()) {
			userAuthorities.add(new SimpleGrantedAuthority(userRoleLiterator
					.next().getAuthority()));
		}

		/* Business Role 추가 */
//		ListIterator<UserRole> businessRoleLiterator = getBusinessRole(username)
//				.listIterator();
//		while (businessRoleLiterator.hasNext()) {
//			userAuthorities.add(new SimpleGrantedAuthority(
//					businessRoleLiterator.next().getAuthority()));
//		}

		try {
			Authentication domainUser = getUser(username);
			Assert.notNull(domainUser);

			boolean enabled = true;
			if (domainUser.isEnabled()) {
				enabled = true;
			} else {
				enabled = false;
			}

			boolean accountNonExpired = true;
			if (!StringUtils.hasLength(domainUser.getUsername())) {
				accountNonExpired = false;
			}

			boolean credentialsNonExpired = true;

			boolean accountNonLocked = true;
			if (domainUser.isAccountNonLocked()) {
				accountNonLocked = false;
			} else {
				accountNonLocked = true;
			}

//			UserRegistration userdata = getUserdata(domainUser.getUsername());
			UserRegistration userdata = null;

			CustomUser user = new CustomUser(domainUser.getUsername(),
					domainUser.getPassword(), enabled, accountNonExpired,
					credentialsNonExpired, accountNonLocked, userAuthorities,
					userdata);
			return user;
		} catch (IllegalArgumentException e) {
			throw new UsernameNotFoundException(e.getMessage());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<UserRole> getUserRole(String username) {
		return iSecurityRepository.getUserRole(username);
	}

	public Authentication getUser(String username) {
		return iSecurityRepository.getUser(username);
	}

//	public UserRegistration getUserdata(String username) {
//		return iSecurityRepository.getUserdata(username);
//	}
//
//	public List<UserRole> getBusinessRole(String username) {
//		return iSecurityRepository.getUserBusinessRole(username);
//	}
}
